package shared.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import shared.mess.Message;
import shared.util.Util;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author akaovi
 * @description 发送信息端
 * @createTime 2022/9/16 15:18
 */
public class Client {
    private InetAddress selfIp;
    private InetAddress targetIp;

    private int port = 17777;

    private static  final Logger logger = LoggerFactory.getLogger(Client.class);

    public Client() {
        try {
            selfIp = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            logger.error("Init Client Error >>> ", e);
        }
    }

    public Client(InetAddress serverIp) {
        this();
        this.targetIp = serverIp;
    }

    public Client(InetAddress serverIp, int port) {
        this(serverIp);
        this.port = port;
    }

    public InetAddress getSelfIp() {
        return selfIp;
    }

    public void setSelfIp(InetAddress selfIp) {
        this.selfIp = selfIp;
    }

    public InetAddress getTargetIp() {
        return targetIp;
    }

    public void setTargetIp(InetAddress targetIp) {
        this.targetIp = targetIp;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void send(Message message) {
        Socket clientSocket = null;
        OutputStream os = null;
        ObjectOutputStream oos = null;
        try {
            clientSocket = new Socket(targetIp, this.port);
            os = clientSocket.getOutputStream();
            oos = new ObjectOutputStream(os);
            oos.writeObject(message);
            oos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            if (clientSocket != null) {
                try {
                    clientSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    @Override
    public String toString() {
        return "Client{" +
                "clientIp=" + selfIp +
                ", serverIp=" + targetIp +
                '}';
    }

    public static void main(String[] args) {
        System.out.print("请输入IP地址: ");
        InetAddress serverIp = Util.getIpAddress();
        Client client = new Client(serverIp);
        System.out.println(client);

        // 文本
//        System.out.print("请输入待发送内容:");
//        String s = Util.getStringInput();
//        Message ms = Util.getTextMessage(s);

        // 文件
        Message ms = Util.getFileMessage("D:\\javaProject\\SharedClipboard\\out\\production\\Shared\\shared.zip");

        // 发送
        client.send(ms);
    }
}
